package com.zx.fm.controller;

import com.zx.common.annotation.Log;
import com.zx.common.core.controller.BaseController;
import com.zx.common.core.domain.AjaxResult;
import com.zx.common.core.page.TableDataInfo;
import com.zx.common.enums.BusinessType;
import com.zx.common.utils.RedisSequenceGenerator;
import com.zx.common.utils.poi.ExcelUtil;
import com.zx.cs.domain.Inventory;
import com.zx.cs.service.IInventoryService;
import com.zx.fm.domain.GoodsLoan;
import com.zx.fm.domain.GoodsLoanVO;
import com.zx.fm.domain.ReimbursementApplicationVO;
import com.zx.fm.service.IGoodsLoanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * -- 高贵的马儿驱逐体内的一切外力 --
 *
 * @author kaajee
 * @date 2025/9/6
 * @desc
 */
@RestController
@RequestMapping("/fm/la")
public class GoodsLoanController extends BaseController {
    @Autowired
    private IGoodsLoanService goodsLoanService   ;
    @Autowired
    private IInventoryService inventoryService;
    // 注入编号生成器
    private final RedisSequenceGenerator sequenceGenerator;

    public GoodsLoanController(RedisSequenceGenerator sequenceGenerator) {
        this.sequenceGenerator = sequenceGenerator;
    }
    @GetMapping("/list")
    public TableDataInfo list(GoodsLoanVO goodsLoanVO) {
        startPage();
        List<GoodsLoanVO> list = goodsLoanService.selectGoodLoanList(goodsLoanVO);
        return getDataTable(list);
    }

    @GetMapping("/get/{id}")
    public AjaxResult getGoodLoanById(@PathVariable("id") Long id) {
        GoodsLoanVO goodsLoanVO = goodsLoanService.selectById(id);
        return success(goodsLoanVO);
    }
    @PreAuthorize("@ss.hasPermi('fm:la:add')")
    @Log(title = "借货管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addGoodLoan(@Validated @RequestBody GoodsLoanVO goodsLoan) {
        Long itemId = goodsLoan.getItemId();
        System.out.println("itemId = " + itemId);
        if (inventoryService.selectInventoryById(goodsLoan.getItemId()).getQuantity() < goodsLoan.getItemQty()){
            System.out.println("goodsLoan = " + goodsLoan);
            return error("物品库存不足");
        }
        Inventory inventory = inventoryService.selectInventoryById(itemId);
        inventory.setQuantity(inventory.getQuantity() - goodsLoan.getItemQty());
        inventoryService.updateInventory(inventory);
        goodsLoan.setUserId(getUserId());
        goodsLoan.setLoanTime(new Date());
        goodsLoan.setLoanNo(sequenceGenerator.generateSequence("LOAN"));
        return success(goodsLoanService.insert(goodsLoan));
    }
    @PreAuthorize("@ss.hasPermi('fm:la:edit')")
    @Log(title = "借货管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult editGoodLoan(@RequestBody GoodsLoanVO goodsLoan) {
        goodsLoan.setUserId(getUserId());
        return success(goodsLoanService.updateById(goodsLoan));
    }
    @PreAuthorize("@ss.hasPermi('fm:la:del')")
    @Log(title = "借货管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/del/{id}")
    public AjaxResult delGoodLoan(@PathVariable("id") Long id) {
        return success(goodsLoanService.deleteById(id));
    }
    @PreAuthorize("@ss.hasPermi('fm:la:export')")
    @Log(title = "借货管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GoodsLoanVO goodsLoanVO) {
        List<GoodsLoanVO> list = goodsLoanService.selectGoodLoanList(goodsLoanVO);
        ExcelUtil<GoodsLoanVO> util = new ExcelUtil<>(GoodsLoanVO.class);
        util.exportExcel(response, list, "借货申请数据");
    }
}
